<?php namespace App\Http\Controllers;
use App\Commodity;
use App\Commodityrecord;
use App\House;
use App\Template;
use App\Node;
use Illuminate\Support\Facades\Auth;
use Input;
use Excel;
use DB;
use Redirect;
use Illuminate\Http\Request;
use App\Process;
use App\Thread;
use App\Unit;
use App\Userprocess;
use Illuminate\Contracts\Auth\Guard;
class FlowsController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Welcome Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders the "marketing page" for the application and
	| is configured to only allow guests. Like most of the other sample
	| controllers, you are free to modify or remove it as you desire.
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
    public $ACT;
    public $USER;
    protected $auth;
	public function __construct(Guard $auth)
	{
        $this->auth = $auth;
        $this->USER = Auth::user();
        $this->ACT = 'commodities';
//		$this->middleware('auth');
	}

	/**
	 * Show the application welcome screen to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
echo 111;

	}


    public function store(){
//        var_dump(Input::all());
        $names = Input::get('name');
//        var_dump($names);
        $count = count($names);

        //step查询模板信息, 生成流程
        $template = Template::find(Input::get('template_id'));



        //
        $nodes = Node::where('template_id',Input::get('template_id'))->orderBy('step','asc')->get();
        $last_node = Node::where('template_id',Input::get('template_id'))->where('step',2)->first();
        $next_unit_id = Input::get('unit_id');
        if(!$next_unit_id){
            $next_unit_id = $this->USER->unit_id;
            $next_unit_type = $this->USER->unit->type;
        }else{
            $unit = Unit::find($next_unit_id);
            $next_unit_type = $last_node->unit_type;
        }



        $process = new Process();
        $process->des = $template->name;
        $process->user_id = $this->USER->id;
        $process->unit_id = $next_unit_id;
        $process->next_unit_type = $next_unit_type;
        $process->next_unit_id = $next_unit_id;
        $process->use_id = $last_node->use_id;
        $process->use_type = $last_node->type;
        $process->thread_id = 0;
        $process->step = 2;
        $process->template_id = Input::get('template_id');
        $process->status = 1;
        $process->save();

        //查询节点信息，生成进程


        foreach($nodes as $key => $node){
            if($key == 0){
                $threads[] = array(
                    'process_id' => $process->id,
                    'user_id' => $this->USER->id,
                    'use_id' => $this->USER->id,
                    'node_id' => $node->id,
                    'step' => $node->step,
                    'des' => '',
                    'type' => 'user',
                    'unit_id' => 0,
                    'status' => 2
                );
            }elseif($key == 1){
                $threads[] = array(
                    'process_id' => $process->id,
                    'use_id' => $node->use_id,
                    'node_id' => $node->id,
                    'step' => $node->step,
                    'user_id' => 0,
                    'type' => $node->type,
                    'des' => $node->des,
                    'unit_id' => 0,
                    'status' => 1
                );
            }else{
                $threads[] = array(
                    'process_id' => $process->id,
                    'use_id' => $node->use_id,
                    'node_id' => $node->id,
                    'step' => $node->step,
                    'user_id' => 0,
                    'type' => $node->type,
                    'des' => $node->des,
                    'unit_id' => 0,
                    'status' => 0
                );
            }
        }
        $id = DB::table('threads')->insert($threads);


        $prices = Input::get('price');
        $commodity_ids = Input::get('name');
        $counts = Input::get('count');
        $gongyings = Input::get('gongying');
        $process_remark = '';
        for($i = 0; $i < $count; $i++){
            $process_remark .=$names[$i]."*".$counts[$i].';';
            if(!$prices){
                $commodity = Commodity::find($commodity_ids[$i]);
                $price = $commodity->price;
            }else{
                $price = $prices[$i];
            }

            if($template->unit_type == 0){
                $commodityrecords[] = array(
                    'process_id' => $process->id,
                    'unit_id' => $this->USER->unit_id,
                    'commodity_id' => $commodity_ids[$i],
                    'count' => $counts[$i],
                    'price' => $price,
                    'type' => $template->action_type,
                    'status' => 0,
                    'remark' => $gongyings[$i]
                );
            }
            elseif($template->action_type == -1){
                $commodityrecords[] = array(
                    'process_id' => $process->id,
                    'unit_id' => $this->USER->unit_id,
                    'commodity_id' => $commodity_ids[$i],
                    'count' => $counts[$i],
                    'price' => $price,
                    'type' => $template->action_type,
                    'status' => 0,
                );
            }
            elseif($template->unit_type == 1){
                $commodityrecords[] = array(
                    'process_id' => $process->id,
                    'unit_id' => $this->USER->unit_id,
                    'commodity_id' => $commodity_ids[$i],
                    'count' => $counts[$i],
                    'price' => $price,
                    'type' => '-'.$template->action_type,
                    'status' => 0,
                );
                $commodityrecords2[] = array(
                    'process_id' => $process->id,
                    'unit_id' => Input::get('unit_id'),
                    'commodity_id' => $commodity_ids[$i],
                    'count' => $counts[$i],
                    'price' => $price,
                    'type' => $template->action_type,
                    'status' => 0,
                );
            }

        }
        $id2 = DB::table('commodityrecords')->insert($commodityrecords);
        if($template->unit_type == 1 && !empty($commodityrecords2)){
            $id3 = DB::table('commodityrecords')->insert($commodityrecords2);
        }

        $userprocess = New Userprocess();
        $userprocess->user_id = $this->USER->id;
        $userprocess->process_id = $process->id;
        $userprocess->save();

        $update_process_remark = Process::find($process->id);
        $update_process_remark->remark = $process_remark;
        $update_process_remark->save();



    }
    public function show($id)
    {

        $ACT = $this->ACT;
        $process = Process::find($id);
        $this_thread = Thread::where('step',$process->step)->where('process_id',$process->id)->first();

        $nodes = Node::where('template_id',$process->template->id)->orderBy('step','asc')->get();
        $records = Commodityrecord::where('process_id',$id)->where('unit_id',$process->unit_id)->get();

        $threads = Thread::where('process_id',$id)->where('status','>',1)->orderBy('step','asc')->get();

        if($process->template->type == 'commodity'){
            $METHOD = 'flows';
            return view('flows.show_action_commodity',compact('ACT','METHOD','process','this_thread','threads','nodes','records'));
        }
    }
    public function create()
    {

    }

    /**
     * Show the form for editing the specified resource.
     * GET /mounts/{id}/edit
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        echo 111;
        echo $id.'edit';
    }
    public function launch($id)
    {
        $template = Template::find($id);
        $nodes = Node::where('template_id',$id)->orderBy('step','asc')->get();
        $ACT = $this->ACT;
        $METHOD = 'flows';

        $units = Unit::where('father_id',$this->USER->unit_id)->get();
        return view('flows.launch',compact('ACT','METHOD','template','nodes','units'));
    }


    public function update($id)
    {
        $action = Input::get('action');
        if('通过' == $action){
            DB::beginTransaction();
            $process = Process::find($id);
            $thread = Thread::where('step',$process->step)->where('process_id',$process->id)->first();
            $all_step = Node::where('template_id',$process->template->id)->orderBy('step','asc')->count();
            $user_exist = Userprocess::where('user_id', $this->USER->id)->where('process_id', $process->id)->count();


            if($all_step == $thread->step){//为最后一步
                $process->status = 2;
                $process->save();

                $thread->status = 2;
                $thread->save();



                if($process->template->type == 'commodity'){
                    $commodityrecords = Commodityrecord::where('process_id',$process->id)->get();
                    foreach($commodityrecords as $commodityrecord){
                        $commodity_count = House::where('unit_id',$commodityrecord->unit_id)->where('commodity_id',$commodityrecord->commodity_id)->where('status',1)->count();
                        if($commodity_count > 0){
                            $House = House::where('unit_id',$commodityrecord->unit_id)->where('commodity_id',$commodityrecord->commodity_id)->where('status',1)->first();
                            if($commodityrecord->type == 1){
                                $House->count = $House->count + $commodityrecord->count;
                            }else{
                                $House->count = $House->count - $commodityrecord->count;
                            }
                            $House->save();
                        }else{
                            $House = new House();
                            $House->commodity_id = $commodityrecord->commodity_id;
                            $House->unit_id = $commodityrecord->unit_id;
                            $House->count = $commodityrecord->count;
                            $House->status = 1;
                            $House->save();
                        }
                        $c_record = Commodityrecord::find($commodityrecord->id);
                        $c_record->status = 1;
                        $c_record->save();
                    }
                }
            }else{//不是最后一步
                $thread->status = 2;
                $thread->user_id =  $this->USER->id;
                $thread->save();
                $next_step = $process->step+1;
                $next_thread = Thread::where('step',$next_step)->where('process_id',$process->id)->first();
                $next_thread->status = 1;
                $next_thread->save();

                $process->next_unit_type = $next_thread->node->unit_type ;
                $process->use_id = $next_thread->use_id;
                $process->use_type = $next_thread->type;
                $process->step = $next_thread->step;
                $process->save();
            }

            if($user_exist == 0){
                $userprocess = New Userprocess();
                $userprocess->user_id = $this->USER->id;
                $userprocess->process_id = $process->id;
                $userprocess->save();
            }
            DB::commit();

        }

        var_dump(Input::all());
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /mounts/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
    public function upload(){
        $ACT = $this->ACT;
        return view('sources.upload',compact('ACT'));
    }

}
